package com.sduept.nwld.dataserver.controller.daily;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.cim.ledger.manager.SubstationLedgerManager;
import com.sduept.cim.ledger.model.SubstationLedger;
import com.sduept.cim.model.core.SubControlArea;
import com.sduept.cim.model.core.Substation;
import com.sduept.core.entity.SystemParameter;
import com.sduept.message.manager.AlarmMessageCheckManager;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.nwld.dataserver.manager.protection.AlarmMessageReceiverManager;
import com.sduept.nwld.dataserver.manager.protection.AlarmReceiveRoleManager;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import com.sduept.permission.entity.DepartmentEntityBean;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.manager.DepartManager;
import com.sduept.utils.PingYinUtil;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

@Named
@ViewScoped
public class AlarmMessageReceiverController extends AbstractController {

	private static final long serialVersionUID = 555393739962262785L;
	@Autowired
	private CimManager cimM;
	@Autowired
	private AlarmMessageReceiverManager alarmMsgReceiverM;
	@Autowired
	private SubstationLedgerManager stationLedgerM;
	@Autowired
	private AlarmReceiveRoleManager arm;
	@Autowired
	private AlarmMessageCheckManager alarmMsgM;
	@Autowired
	private DepartManager dm;
	@Autowired
	private AlarmMessageReceiverManager alarmMessageReceiverManager;
	
	private String ctrlArea;
	private TreeNode areaRoot; //控制区域树
	private TreeNode areaSelectedNode;
	private List<Substation> subStations = new ArrayList<Substation>();
	private List<SubControlArea> subControlAreas = new ArrayList<SubControlArea>();
	private SubControlArea selectSubCtrlArea = new SubControlArea();
	
	private String voltage = "500"; //电压等级默认为500
	private String msgTypeQuery;
	private String stationQuery;
	
	private List<SystemParameter> msgTypes = new ArrayList<SystemParameter>();
	
	@PostConstruct
	public void init() {
		subControlAreas = cimM.findAllSubControlAreas();
		initAreaTree(subControlAreas);
		msgTypes = arm.initAlarmParams();
		if(msgTypes.size()>0) {
			msgTypeQuery = msgTypes.get(0).getId();
		}
	}
	
	public List<String> completeCtrlArea(String query) {
		List<String> qualifiedEquipmentNames = new ArrayList<String>();
        for (SubControlArea area : subControlAreas) {
        	String pName = PingYinUtil.getPingYinHeader(area.getName());
        	if (pName.contains(query)) {
        		qualifiedEquipmentNames.add(area.getName());
        	}
        }
		return qualifiedEquipmentNames;
	}
	
	public void autoComplete(SelectEvent event) {
		Object item = event.getObject();
		if (null != item) {
			filterTree(item.toString());
		}
	}
	
	private void filterTree(String name) {
		areaRoot = new DefaultTreeNode();
		List<SubControlArea> subControlAreas = cimM.findsubControlAreasByName(name);
		initAreaTree(subControlAreas);
	}
	
	public void initAreaTree(List<SubControlArea> subControlAreas) {
		areaRoot = new LibTreeNode("Root", null);
		for (SubControlArea s : subControlAreas) {
			new LibTreeNode("subControlArea",s,areaRoot);
		}
	}
	
	public void refresh() {
		ctrlArea = "";
		subControlAreas = cimM.findAllSubControlAreas();
		initAreaTree(subControlAreas);
	}
	
	/**
	 * 控制区域树选择
	 * @param event
	 */
	public void onNodeSelect(NodeSelectEvent event) {
		subStations.clear();
		areaSelectedNode = event.getTreeNode();
		selectSubCtrlArea = (SubControlArea) areaSelectedNode.getData();
		query();
	}
	
	// 根据管控区域、电压等级获取变电站
	public void query() {
		subStations = alarmMsgReceiverM.findSubstation(stationQuery,selectSubCtrlArea.getRdfId(), voltage);
	}
	
	/**
	 * 获取当前变电站的调管单位
	 */
	public String getDispatchUnit(String stationId) {
		SubstationLedger ledger = stationLedgerM.findSubstationLedger(stationId);
		if(null != ledger) {
			return ledger.getDispatchUnit();
		} else {
			return null;
		}
	}
	
	/**
	 * 获取当前变电站的运维单位
	 */
	public String getMaintenanceUnit(String stationId) {
		SubstationLedger ledger = stationLedgerM.findSubstationLedger(stationId);
		if(null != ledger) {
			return ledger.getMaintenanceUnit();
		} else {
			return null;
		}
	}

	/**
	 * 获取当前变电站的短信接收人员
	 * @return 
	 */
	public List<String> getMsgReceivers(Substation station) {
		List<String> resultList = new ArrayList<String>();
		Map<String, PersonnelEntityBean> pMap = alarmMessageReceiverManager.findAllMsgReceiver(msgTypeQuery, station.getId());
		if(pMap == null || pMap.size() == 0){
			return null;
		}
		List<DepartmentEntityBean> pDepts = null;
		PersonnelEntityBean person = null;
		String string = null;
		for (Entry<String, PersonnelEntityBean> entry : pMap.entrySet()) {
			// 根据person的id获取父部门，如：/南方电网/广东电网/广东中调/某某某
			person = entry.getValue();
			pDepts = dm.recursiveFindParentDepartments(person);
			string = "";
			for (DepartmentEntityBean dept : pDepts) {
				string+="/"+dept.getName();
			}
			string+="/"+person.getName();
			resultList.add(string);
		}
		return resultList;
	}
	
	public List<Substation> getSubStations() {
		return subStations;
	}

	public void setSubStations(List<Substation> subStations) {
		this.subStations = subStations;
	}

	public SubControlArea getSelectSubCtrlArea() {
		return selectSubCtrlArea;
	}

	public void setSelectSubCtrlArea(SubControlArea selectSubCtrlArea) {
		this.selectSubCtrlArea = selectSubCtrlArea;
	}

	public String getVoltage() {
		return voltage;
	}

	public void setVoltage(String voltage) {
		this.voltage = voltage;
	}

	public TreeNode getAreaRoot() {
		return areaRoot;
	}

	public void setAreaRoot(TreeNode areaRoot) {
		this.areaRoot = areaRoot;
	}

	public TreeNode getAreaSelectedNode() {
		return areaSelectedNode;
	}

	public void setAreaSelectedNode(TreeNode areaSelectedNode) {
		this.areaSelectedNode = areaSelectedNode;
	}

	public String getCtrlArea() {
		return ctrlArea;
	}

	public void setCtrlArea(String ctrlArea) {
		this.ctrlArea = ctrlArea;
	}

	public List<SystemParameter> getMsgTypes() {
		return msgTypes;
	}

	public void setMsgTypes(List<SystemParameter> msgTypes) {
		this.msgTypes = msgTypes;
	}

	public String getMsgTypeQuery() {
		return msgTypeQuery;
	}

	public void setMsgTypeQuery(String msgTypeQuery) {
		this.msgTypeQuery = msgTypeQuery;
	}

	public String getStationQuery() {
		return stationQuery;
	}

	public void setStationQuery(String stationQuery) {
		this.stationQuery = stationQuery;
	}

}
